﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace UIFrame
{
    //一级菜单面板
    public class BasePanel : MonoBehaviour
    {
        [Header("BasePanel")]       
        public List<CommonItem> ListCommonItem;          //热点组     
        //存放Common的事件
        protected Dictionary<string, UnityAction> DicItemEvent = new Dictionary<string, UnityAction>();

        public virtual void Reset()
        {
            ListCommonItem.Clear();
            ListCommonItem.AddRange(GetComponentsInChildren<CommonItem>());
        }

        protected virtual void Awake()
        {           
            Init();
        }

        public void Init()
        {
            AddEvent();
            for (int i = 0; i < ListCommonItem.Count; i++)
            {
                string key = ListCommonItem[i].name;
                if (DicItemEvent.ContainsKey(key))
                {
                    ListCommonItem[i].RegisterEvent(DicItemEvent[key]);
                }
            }
        }

        /// <summary>
        /// 添加事件
        /// </summary>
        protected virtual void AddEvent()
        {

        }

        protected virtual void OnEnable()
        {
            RegisterCommon();
            KeyTab.Instance.RegisterEscAction(() =>
            {
                UIManager.Instance.BackLastPanel();
            });
        }

        public void RegisterCommon()
        {
            KeyTab.Instance.RegisterCommon(ListCommonItem);
        }

        /// <summary>
        /// 通用的返回
        /// </summary>
        protected virtual void EventBack()
        {
            UIManager.Instance.BackLastPanel();
        }

        /// <summary>
        /// 开启显示
        /// </summary>
        public void Show()
        {
            //暂时没动画
            gameObject.SetActive(true);
        }

        /// <summary>
        /// 关闭显示
        /// </summary>
        public void Close()
        {
            //暂时没动画
            gameObject.SetActive(false);
        }
    }
}